package demo03

import scala.util.control.Breaks
import scala.util.control.Breaks._

/**
 * @author xiaoZhao
 * @date 2022/6/24
 * @describe
 */
object Test06_Break {
  def main(args: Array[String]): Unit = {
    //    抛出异常的方式中断循环
    try {
      for (i <- 0 until 5) {
        if (i == 3)
          throw new RuntimeException
        println(i)
      }
    } catch {
      case e: Exception => // 退出循环
    }


    // 2.使用scala中的Breaks中的break方法，实现异常的抛出和捕捉
    Breaks.breakable(
      for (i <- 0 until 5) {
        if (i == 3)
          Breaks.break()
        println(i)
      }
    )

    breakable(
      for (i <- 0 until 5) {
        if (i == 3)
          break()
        println(i)
      }
    )
    println("这是循环外的代码")
  }
}
